package com.controller;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.model.Article;
import com.model.Question;
import com.model.Tag;
import com.model.User;
import com.service.ArticleService;
import com.service.QuestionService;
import com.service.UserService;
import com.util.Page;
import com.util.TimeEvent;

@Controller
@RequestMapping("/u")
public class HomeController {
	@Autowired
	private UserService userService;

	@Autowired
	private QuestionService questionService;

	@Autowired
	private ArticleService articleService;

	@RequestMapping(value = "/home", method = RequestMethod.GET)
	public ModelAndView index(HttpSession session, ModelAndView mav) {
		User user = (User) session.getAttribute("user");
		List<Tag> taglist = userService.getUserTags(user.getId());
		mav.addObject("user", user);
		mav.addObject("tags", taglist);
		mav.setViewName("u_home");
		return mav;
	}

	@RequestMapping(value = "/answerd", method = RequestMethod.GET)
	public ModelAndView answered(HttpSession session, ModelAndView mav,
			@RequestParam(value = "page", required = false) Integer page) {
		User user = (User) session.getAttribute("user");
		if (null == page) {
			page = 1;
		}
		List<Tag> taglist = userService.getUserTags(user.getId());
		Page answerPage = questionService.getAnswerdQuestionByPage(user.getId(), page, 15);
		mav.addObject("user", user);
		mav.addObject("tags", taglist);
		mav.addObject("answers", answerPage);
		mav.setViewName("u_answerd");
		return mav;
	}

	@RequestMapping(value = "/question", method = RequestMethod.GET)
	public ModelAndView unAnswered(HttpSession session, ModelAndView mav,
			@RequestParam(value = "page", required = false) Integer page) {
		User user = (User) session.getAttribute("user");
		if (null == page) {
			page = 1;
		}
		List<Tag> taglist = userService.getUserTags(user.getId());
		Page answerPage = questionService.getUnAnswerdQuestionByPage(user.getId(), page, 15);
		mav.addObject("user", user);
		mav.addObject("tags", taglist);
		mav.addObject("answers", answerPage);
		mav.setViewName("u_question");
		return mav;
	}
	
	@RequestMapping(value = "/squestion", method = RequestMethod.GET)
	public ModelAndView sendQuestions(HttpSession session, ModelAndView mav,
			@RequestParam(value = "page", required = false) Integer page) {
		User user = (User) session.getAttribute("user");
		if (null == page) {
			page = 1;
		}
		List<Tag> taglist = userService.getUserTags(user.getId());
		Page questionPage = questionService.findSendQuestionByPage_All(user.getId(), page, 15);
		mav.addObject("user", user);
		mav.addObject("tags", taglist);
		mav.addObject("questions", questionPage);
		mav.setViewName("u_squestion");
		return mav;
	}

	@RequestMapping(value = "/ignore", method = RequestMethod.GET)
	public ModelAndView ignored(HttpSession session, ModelAndView mav,
			@RequestParam(value = "page", required = false) Integer page) {
		User user = (User) session.getAttribute("user");
		if (null == page) {
			page = 1;
		}
		List<Tag> taglist = userService.getUserTags(user.getId());
		Page questionPage = questionService.getIgnoredQuestion(user.getId(), page, 15);
		mav.addObject("user", user);
		mav.addObject("tags", taglist);
		mav.addObject("questions", questionPage);
		mav.setViewName("u_ignore");
		return mav;
	}

	@RequestMapping(value = "/self", method = RequestMethod.GET)
	public ModelAndView article(HttpSession session, ModelAndView mav,
			@RequestParam(value = "page", required = false) Integer page) {
		User user = (User) session.getAttribute("user");
		if (null == page) {
			page = 1;
		}
		List<Tag> taglist = userService.getUserTags(user.getId());
		Page articlePage = articleService.findPageByUserId(user.getId(), page, 15);
		mav.addObject("user", user);
		mav.addObject("tags", taglist);
		mav.addObject("articles", articlePage);
		mav.setViewName("u_self");
		return mav;
	}
	@RequestMapping(value = "/collect", method = RequestMethod.GET)
	public ModelAndView userCollect(HttpSession session, ModelAndView mav,
			@RequestParam(value = "page", required = false) Integer page){
		
		User user = (User) session.getAttribute("user");
		if (null == page) {
			page = 1;
		}
		
		List<Tag> taglist = userService.getUserTags(user.getId());
		Page pageobj = userService.getUserCollect(user.getId(), page, 15);
		mav.addObject("user", user);
		mav.addObject("tags", taglist);
		mav.addObject("collects", pageobj);	
		mav.setViewName("u_collect");
		return mav;	
		
	}

	@RequestMapping(value = "/follow", method = RequestMethod.GET)
	public ModelAndView myAttension(HttpSession session, ModelAndView mav,
			@RequestParam(value = "page", required = false) Integer page) {
		User user = (User) session.getAttribute("user");
		if (null == page) {
			page = 1;
		}
		List<Tag> taglist = userService.getUserTags(user.getId());
		Page userPage = userService.findMyAttensionForPage(user.getId(), page, 16);
		mav.addObject("user", user);
		mav.addObject("tags", taglist);
		mav.addObject("users", userPage);
		mav.setViewName("u_follow");
		return mav;
	}

	@RequestMapping(value = "/fans", method = RequestMethod.GET)
	public ModelAndView attensionMe(HttpSession session, ModelAndView mav,
			@RequestParam(value = "page", required = false) Integer page) {
		User user = (User) session.getAttribute("user");
		if (null == page) {
			page = 1;
		}
		List<Tag> taglist = userService.getUserTags(user.getId());
		Page userPage = userService.findAttensiondMeForPage(user.getId(), page, 16);
		mav.addObject("user", user);
		mav.addObject("tags", taglist);
		mav.addObject("users", userPage);
		mav.setViewName("u_fans");
		return mav;
	}
	
	@RequestMapping(value = "/active", method = RequestMethod.GET)
	public ModelAndView activeUser(HttpSession session, ModelAndView mav,
			@RequestParam(value = "page", required = false) Integer page) {
		User user = (User) session.getAttribute("user");
		if (null == page) {
			page = 1;
		}
		List<Tag> taglist = userService.getUserTags(user.getId());
		Page userPage = userService.findActiveUserByPage(page, 30);
		mav.addObject("user", user);
		mav.addObject("tags", taglist);
		mav.addObject("users", userPage);
		mav.setViewName("active_user");
		return mav;
	}

	@RequestMapping(value = "/timeline", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> getUserTimeline(HttpSession session,
			@RequestParam("lasttime") long lasttime) throws ParseException {
		Map<String, Object> result = new HashMap<String, Object>();
		User user = (User) session.getAttribute("user");
		List<TimeEvent> list = userService.getUserTimeline(user.getId(), lasttime, 15);
		result.put("ok", true);
		result.put("data", list);
		result.put("msg", "");
		return result;
	}

	@RequestMapping(value = "/submit-answer", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> answerQuestion(@RequestBody Map map, HttpSession session) {
		Map<String, Object> result = new HashMap<String, Object>();
		int questionId = (Integer) map.get("questionId");
		String answer = (String) map.get("answer");
		User user = (User) session.getAttribute("user");
		Question q = questionService.answerQuestion(questionId, answer, user.getId());
		if(user.getQuestionsNum()>0){
			user.setQuestionsNum(user.getQuestionsNum() - 1);
		}
		if (q != null) {
			result.put("ok", true);
			result.put("msg", "");
		}
		else {
			result.put("ok", false);
			result.put("msg", "error");
		}
		return result;
	}

	@RequestMapping(value = "/ignore-question", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> ignoreQuestion(@RequestBody Map map, HttpSession session) {
		Map<String, Object> result = new HashMap<String, Object>();
		int questionId = (Integer) map.get("questionId");
		User user = (User) session.getAttribute("user");
		boolean flag = questionService.ignoreQuestion(questionId, user.getId());
		if (flag) {
			user.setQuestionsNum(user.getQuestionsNum() - 1);
		}
		result.put("ok", flag);
		result.put("msg", "");
		return result;
	}

	@RequestMapping(value = "/unignore-question", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> unignoreQuestion(@RequestBody Map map, HttpSession session) {
		Map<String, Object> result = new HashMap<String, Object>();
		int questionId = (Integer) map.get("questionId");
		User user = (User) session.getAttribute("user");
		boolean flag = questionService.unignoreQuestion(questionId, user.getId());
		if (flag) {
			user.setQuestionsNum(user.getQuestionsNum() + 1);
		}
		result.put("ok", flag);
		result.put("msg", "");
		return result;
	}
	
    @RequestMapping(value = "/publish-article", method = RequestMethod.POST)
   	@ResponseBody
    public Map<String, Object> publishArticle(@RequestBody Map map, HttpSession session){
    	Map<String, Object> result = new HashMap<String, Object>();
		User user = (User) session.getAttribute("user");
		int userId = user.getId();
		String title = (String) map.get("title");
		String content = (String) map.get("content");
		boolean isPublic = (Boolean) map.get("ispublic");
		int is_public = isPublic?1:0;
		ArrayList tags = (ArrayList) map.get("tags");
		Article article = new Article();
		article.setTitle(title);
		article.setContent(content);
		article.setUser(user);
		article.setAgreeNum(0);
		article.setCollectNum(0);
		article.setDisagreeNum(0);
		article.setFavorNum(0);
		article.setCollectNum(0);
		article.setIsPublic(is_public);
		articleService.addArticle(article, tags);
		result.put("ok", true);
		result.put("msg", "");
		return result;
    }
}
